package com.gorkr.labuladong.data_struct.array;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * 顺序 正方形矩阵
 * @author gorkr
 * @date 2022/06/05 11:10
 **/
public class M59SpiralMatrixII {

    public int[][] generateMatrix(int n) {

        int[][] res = new int[n][n];
        int size = 0;

        int upperBound = 0, lowerBound = n-1;
        int leftBound = 0, rightBound = n-1;

        while (size<n*n){
            if(upperBound<=lowerBound){
                for (int i = leftBound; i <= rightBound; i++) {
                    res[upperBound][i] = ++size;
                }
                upperBound++;
            }
            if(leftBound<=rightBound){
                for (int i = upperBound; i <= lowerBound; i++) {
                    res[i][rightBound] = ++size;
                }
                rightBound--;
            }
            if(upperBound<=lowerBound){
                for (int i = rightBound; i >=leftBound ; i--) {
                    res[lowerBound][i] = ++size;
                }
                lowerBound--;
            }
            if(leftBound<=rightBound){
                for (int i = lowerBound; i >=upperBound ; i--) {
                    res[i][leftBound] = ++size;
                }
                leftBound++;

            }
        }

        return res;
    }


    @Test
    public void test(){
        int n = 1;
        System.out.println(Arrays.deepToString(generateMatrix(n)));
    }
}
    